import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 年年 
 */
///////////////////////////////////////////////////////////////////////////
// IO工具类
///////////////////////////////////////////////////////////////////////////
public class IOUtils {

    /**
     * 字节流
     */
    public static void outPutUtil(String path,byte[] b){
        try(FileOutputStream outputStream = new FileOutputStream(path)){
            outputStream.write(b);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void outPutUtil(String path,byte[] b,boolean append){
        try(FileOutputStream outputStream = new FileOutputStream(path,append)){
            outputStream.write(b);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static byte[] inputUtil(String path){
        try(FileInputStream inputStream = new FileInputStream(path)) {
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
            return bytes;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 字符流：
     */

    public static List<Character> ReaderChar(String path){
        List<Character> list = new ArrayList<>();
        try(FileReader r = new FileReader(path)){
            int tmp;
            while ((tmp=r.read())!=-1){
                list.add((char)tmp);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        return list;
    }

    public static void writeChar(String path, List<Character> c){
        try(FileWriter w = new FileWriter(path)){
            for (char cs : c) {
                w.append(cs);
            }
            w.flush();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    /**
     * 文件拷贝
     * @// TODO: 2022/12/27
     */
    public static void copyFile(String file,String path){
        outPutUtil(path, IOUtils.inputUtil(file));
        System.out.println("文件拷贝成功!");
    }

    /**
     * 缓冲字节流
     *
     * @author 年年
     */

    public static byte[] bufferInput(String path){
        try(BufferedInputStream bufferedInputStream = new BufferedInputStream(Files.newInputStream(Paths.get(path)))) {
            byte[] bytes = new byte[bufferedInputStream.available()];
            bufferedInputStream.read(bytes);
            return bytes;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void bufferOutput(String path,byte[]content){
        try(BufferedOutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(Paths.get(path)))){
            outputStream.write(content);
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    /**
     * 缓冲字符流
     *
     * @return
     */
    public static String bufferReader(String path){
        try(BufferedReader r = new BufferedReader(new FileReader(path))){
            return r.readLine();
        }catch (IOException e){
            e.printStackTrace();
        }
        throw new RuntimeException();
    }


    public static void bufferWrite(String path,String content){
        try (BufferedWriter w = new BufferedWriter(new FileWriter(path))){
            w.write(content);
            w.flush();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void bufferWrite(String path,String content,boolean append){
        try (BufferedWriter w = new BufferedWriter(new FileWriter(path,append))){
            w.write(content);
            w.flush();
        }catch (IOException e){
            e.printStackTrace();
        }
    }





}
